﻿using Microsoft.Xaml.Behaviors;
using System.Windows;
using System.Windows.Controls;

namespace TestFireBroadcastingHostPhone
{
    public class TextBoxHelper : Behavior<TextBox>
    {
        public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.Register("IsFocused", typeof(bool), typeof(TextBoxHelper), new PropertyMetadata(false, OnIsFocusedChanged));
        public bool IsFocused
        {
            get { return (bool)GetValue(IsFocusedProperty); }
            set { SetValue(IsFocusedProperty, value); }
        }

        private static void OnIsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var behavior = d as TextBoxHelper;
            if (behavior != null && behavior.AssociatedObject != null)
            {
                behavior.SetFocus((bool)e.NewValue);
            }
        }

        private void SetFocus(bool isFocused)
        {
            if (isFocused && AssociatedObject != null)
            {
                AssociatedObject.Focus();
            }
        }
    }
}
